package com.tzbank.wisdom.service.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.String2Utils;
import com.tzbank.wisdom.dao.IbsaccessLogMapper;
import com.tzbank.wisdom.entity.IbsaccessLog;
import com.tzbank.wisdom.service.IbsaccessLogService;
import com.tzbank.wisdom.utils.BusinessConstant;

import net.sf.json.JSONObject;

@Service
public class IbsaccessLogServiceImpl implements IbsaccessLogService {

	@Resource
	private IbsaccessLogMapper ibsaccessLogMapper;

	@Transactional
	@Override
	public boolean add(Map<String, String> reqBody, Map<String, Object> loginResult) {

		// Android ios
		String deviceType = reqBody.get("deviceType");

		String ip = reqBody.get("ip");

		JSONObject dataLogin = (JSONObject) loginResult.get("data");

		String clientSeqNo = dataLogin.getString("clientSeqNo");
		String userSeqNo = dataLogin.getString("userSeqNo");

		IbsaccessLog log = new IbsaccessLog();

		if (CodeUtil.CODE_200.getCode().equals(loginResult.get("code"))) {
			log.setAccessstate("1");// 是否登录成功： 1-成功 2-失败
		} else {
			if (BusinessConstant.FIRST_LOGIN_CODE.equals(loginResult.get("code"))) {
				log.setAccessstate("3");
				log.setUseragent("首次登陆");
			}else {
				log.setAccessstate("2");
			}
		}

		log.setAccessjnlno("LOG" + String2Utils.getGenerateUUID());
		log.setAccessdate(new Date());
		log.setMachineid("mbbs"); // 智脑
		log.setAccessip(ip); // IP 地址
		log.setModuleid("per");
		log.setChannelid(deviceType);
		log.setLogintype("R");
		log.setUserseq(userSeqNo);
		log.setUniqueuserid(clientSeqNo);

		int res = ibsaccessLogMapper.inserSelective(log);
		return res > 0;
	}

}
